package ContainerPractise;


import java.util.*;

/**
 * User: yuxinlei
 * Date: 7/28/11
 * Time: 4:42 PM
 */
public class PerformanceCompact {

    private Set<Integer> hashSet = new HashSet<Integer>();
    private List<Integer> arrayList = new ArrayList<Integer>();
    private List<Integer> linkedList = new LinkedList<Integer>();
    private Set<Integer> linkedHashSet = new LinkedHashSet<Integer>();
    private PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();
    private Long current = new Long(0L);
    TreeMap treeMap;
    HashMap hashMap;
    BitSet bitSet;

    private static final Integer TIMES = 100000;
    private static final Integer LONGTIMES = 1000000;

    public static void main(String[] args) {
        PerformanceCompact performanceCompact = new PerformanceCompact();

        BitSet bitSet1 = new BitSet(3);
        bitSet1.flip(1);
        System.out.println("bitset1:"+bitSet1.get(1));
        System.out.println("bitset2:"+bitSet1.get(2));
        bitSet1.flip(2);
        System.out.println("bitset1:"+bitSet1.get(1));
        System.out.println("bitset2:"+bitSet1.get(2));
        System.out.println("2>>>33:"+(2>>>33));
        System.out.println("2>>33:"+(2>>33));
        System.out.println("2>>65:"+(2>>65));
        System.out.println("1<<6:"+(1<<6));
        System.out.println("((1<<((1l<<6l)-1l))-1):"+(long)((long)((long)(1l<<((1l<<6l)-1l)-1l))));
        System.out.println("((1<<((1l<<6l)-1l))-1)>>62:"+(long)((long)((long)(1l<<((1l<<6l)-1l))-1l)>>62l));
        System.out.println("((1<<((1l<<6l)-1l))-1)>>>62:"+(long)((long)((long)(1l<<((1l<<6l)-1l))-1l)>>>62l));


        System.out.println("!!!Test for forHashSet!!!!");
        performanceCompact.forHashSet();
        System.out.println("!!!Test for forLinkedHashSet!!!!");
        performanceCompact.forLinkedHashSet();
//        System.out.println("!!!Test for forLinkedList!!!!");
//        performanceCompact.forLinkedList();
//        System.out.println("!!!Test for forArrayList!!!!");
//        performanceCompact.forArrayList();
        System.out.println("!!!Test for TreeMap!!!!");
        performanceCompact.forMap(new TreeMap<Integer,Integer>(),"TreeMap");
        System.out.println("!!!Test for HashMap!!!!");
        performanceCompact.forMap(new HashMap<Integer,Integer>(),"HashMap");
        System.out.println("!!!Test for LinkedHashMap!!!!");
        performanceCompact.forMap(new LinkedHashMap<Integer,Integer>(),"LinkedHashMap");
        System.out.println("!!!Test for IdentityHashMap!!!!");
        performanceCompact.forMap(new IdentityHashMap<Integer,Integer>(),"IdentityHashMap");
        System.out.println("!!!Test for WeakHashMap!!!!");
        performanceCompact.forMap(new WeakHashMap<Integer,Integer>(),"WeakHashMap");
        System.out.println("!!!Test for Hashtable!!!!");
        performanceCompact.forMap(new Hashtable<Integer,Integer>(),"Hashtable");

    }

    public void forMap(Map map , String name){
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            map.put(i,i);
        }
        System.out.println(name + " : add  " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            map.get(i);
        }
        System.out.println(name + " : get  " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            map.remove(i);
        }
        System.out.println(name + " : remove  " + (System.currentTimeMillis() - current) + " ");
    }

    public void forHashSet() {
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            hashSet.add(i);
        }
        System.out.println("HashSet : add  " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            hashSet.contains(i);
        }
        System.out.println("HashSet : contain " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            hashSet.remove(i);
        }
        System.out.println("HashSet : remove " + (System.currentTimeMillis() - current) + " ");
        hashSet=null;
    }

    public void forLinkedList() {
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            linkedList.add(i);
        }
        System.out.println("LinkedList :  add " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            linkedList.get(i) ;
        }
        System.out.println("LinkedList : get " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            linkedList.contains(i);
        }
        System.out.println("LinkedList :  contain " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            linkedList.add(1,i);
        }
        System.out.println("LinkedList : insert " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            linkedList.remove(0);
        }
        System.out.println("LinkedList : remove  " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            linkedList.set(i,i);
        }
        System.out.println("LinkedList :  set " + (System.currentTimeMillis() - current) + " ");
//        current = System.currentTimeMillis();
//        for (Integer integer : linkedList) {
//            integer=0;
//        }
//
//        System.out.println("LinkedList : iterator set " + (System.currentTimeMillis() - current) + " ");
//        System.out.println(linkedList.toString());
        linkedList=null;

    }

    public void forArrayList() {
         current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            arrayList.add(i);
        }
        System.out.println("arrayList : add " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            arrayList.get(i) ;
        }
        System.out.println("arrayList :  get " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            arrayList.contains(i);
        }
        System.out.println("arrayList : contain " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            arrayList.add(1,i);
        }
        System.out.println("arrayList : insert " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            arrayList.remove(0);
        }
        System.out.println("arrayList : remove " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < TIMES; i++) {
            arrayList.set(i,i);
        }
        System.out.println("arrayList : set " + (System.currentTimeMillis() - current) + " ");
        arrayList=null;
    }

    public void forLinkedHashSet() {
          current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            linkedHashSet.add(i);
        }
        System.out.println("linkedHashSet : add " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            linkedHashSet.contains(i);
        }
        System.out.println("linkedHashSet : contains " + (System.currentTimeMillis() - current) + " ");
        current = System.currentTimeMillis();
        for (int i = 0; i < LONGTIMES; i++) {
            linkedHashSet.remove(0);
        }
        System.out.println("linkedHashSet : remove " + (System.currentTimeMillis() - current) + " ");
        linkedHashSet=null;
    }


    public Set<Integer> getHashSet() {
        return hashSet;
    }

    public void setHashSet(Set<Integer> hashSet) {
        this.hashSet = hashSet;
    }

    public List<Integer> getArrayList() {
        return arrayList;
    }

    public void setArrayList(List<Integer> arrayList) {
        this.arrayList = arrayList;
    }

    public List<Integer> getLinkedList() {
        return linkedList;
    }

    public void setLinkedList(List<Integer> linkedList) {
        this.linkedList = linkedList;
    }

    public Set<Integer> getLinkedHashSet() {
        return linkedHashSet;
    }

    public void setLinkedHashSet(Set<Integer> linkedHashSet) {
        this.linkedHashSet = linkedHashSet;
    }

    public Long getCurrent() {
        return current;
    }

    public void setCurrent(Long current) {
        this.current = current;
    }
}
